import 'dart:io';

import 'package:clipboard/clipboard.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:travel/routes/index.dart';
import 'package:travel/ui/draw_under_line.dart';
import 'package:travel/utils/colors.dart';
import 'package:travel/utils/styles.dart';
import 'package:travel/utils/tools.dart';
import 'package:travel/screen/flutter_screenutil.dart';

class InfoController extends GetxController {
  RxString avatar = RxString(
      'https://img0.baidu.com/it/u=762633474,1874284403&fm=253&fmt=auto&app=138&f=JPEG?w=505&h=500');

  Rx<XFile> file = Rx(XFile(''));

  RxInt uid = RxInt(4567910);

  RxInt m = RxInt(1);
}

class InfoView extends GetView<InfoController> {
  const InfoView({super.key});

  ImageProvider get getFileImage {
    if (controller.file.value.path.isNotEmpty) {
      return FileImage(File(controller.file.value.path));
    }
    return NetworkImage(controller.avatar.value);
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF7f7),
          appBar: AppBar(
            automaticallyImplyLeading: true,
            leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Icon(
                Icons.arrow_back_ios,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.color3333),
                size: 20.w,
              ),
            ),
            leadingWidth: 40.w,
            title: Text(
              '个人信息',
              style: StylesUtils.customTextStyle(
                  color: ColorsUtils.color3333, fontSize: 18.sp, fontWeight: FontWeight.bold),
            ),
          ),
          body: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 40.h),
                  child: ListBody(
                    children: [
                      Center(
                        child: CircleAvatar(
                          radius: 80,
                          backgroundImage: getFileImage,
                        ),
                      ),
                      Container(
                          margin: EdgeInsets.only(top: 5.h),
                          alignment: Alignment.center,
                          child: TextButton(
                            onPressed: () {
                              _showActionSheet(context);
                            },
                            child: Text(
                              '点击更换头像',
                              style: StylesUtils.customTextStyle(
                                  fontSize: 15.sp, color: ColorsUtils.color6666),
                            ),
                          ))
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 20.w, right: 20.w, top: 15.h),
                  padding: EdgeInsets.symmetric(horizontal: 15.w),
                  decoration:
                      BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.w)),
                  child: ListBody(
                    children: [
                      DrawUnderLine(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                        child: SizedBox(
                          height: 50.h,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '用户姓名',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color3333, fontSize: 15.sp),
                              ),
                              TextButton(
                                  onPressed: () {
                                    Get.toNamed(RoutePath.settingInfoUsername);
                                  },
                                  child: Text.rich(TextSpan(children: [
                                    TextSpan(
                                        text: '晚风吹心里',
                                        style: StylesUtils.customTextStyle(
                                            color: ColorsUtils.color6666, fontSize: 15.sp)),
                                    WidgetSpan(
                                        child: Icon(
                                          Icons.arrow_forward_ios,
                                          color: ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color9999),
                                          size: 15.sp,
                                        ),
                                        alignment: PlaceholderAlignment.middle)
                                  ])))
                            ],
                          ),
                        ),
                      ),
                      DrawUnderLine(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                        child: SizedBox(
                          height: 50.h,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                'UID',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color3333, fontSize: 15.sp),
                              ),
                              TextButton(
                                  onPressed: () {
                                    FlutterClipboard.copy('${controller.uid.value}')
                                        .then((value) => showToast('复制完毕',
                                            context: context, position: StyledToastPosition.bottom))
                                        .catchError((err) => showToast('复制失败',
                                            context: context,
                                            position: StyledToastPosition.bottom));
                                  },
                                  child: Text.rich(TextSpan(children: [
                                    TextSpan(
                                        text: '${controller.uid.value}',
                                        style: StylesUtils.customTextStyle(
                                            color: ColorsUtils.color6666, fontSize: 15.sp)),
                                    WidgetSpan(
                                        child: Icon(
                                          Icons.copy,
                                          color: ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color9999),
                                          size: 15.sp,
                                        ),
                                        alignment: PlaceholderAlignment.middle)
                                  ])))
                            ],
                          ),
                        ),
                      ),
                      DrawUnderLine(
                          color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                          child: SizedBox(
                            height: 50.h,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  '性别',
                                  style: StylesUtils.customTextStyle(
                                      color: ColorsUtils.color3333, fontSize: 15.sp),
                                ),
                                Expanded(
                                    child: Container(
                                  alignment: Alignment.centerRight,
                                  child: Wrap(spacing: 5.w, children: [
                                    TextButton(
                                        onPressed: () {
                                          controller.m.value = 1;
                                        },
                                        child: Text.rich(TextSpan(children: [
                                          WidgetSpan(
                                              child: ToolsUtils.assetImageSvg(controller.m.isOdd
                                                  ? 'u3729_selected.svg'
                                                  : 'u3729.svg'),
                                              alignment: PlaceholderAlignment.middle),
                                          TextSpan(
                                              text: ' 男',
                                              style: StylesUtils.customTextStyle(
                                                  color: controller.m.isOdd
                                                      ? ColorsUtils.color3478
                                                      : ColorsUtils.color6666,
                                                  fontSize: 15.sp))
                                        ]))),
                                    TextButton(
                                        onPressed: () {
                                          controller.m.value = 0;
                                        },
                                        child: Text.rich(TextSpan(children: [
                                          WidgetSpan(
                                              child: ToolsUtils.assetImageSvg(controller.m.isEven
                                                  ? 'u3732_selected.svg'
                                                  : 'u3732.svg'),
                                              alignment: PlaceholderAlignment.middle),
                                          TextSpan(
                                              text: ' 女',
                                              style: StylesUtils.customTextStyle(
                                                  color: controller.m.isEven
                                                      ? ColorsUtils.color3478
                                                      : ColorsUtils.color6666,
                                                  fontSize: 15.sp))
                                        ]))),
                                  ]),
                                ))
                              ],
                            ),
                          )),
                      SizedBox(
                        height: 50.h,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '个性签名',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color3333, fontSize: 16.sp),
                            ),
                            TextButton(
                                onPressed: () {
                                  Get.toNamed(RoutePath.settingInfoSign);
                                },
                                child: Text.rich(TextSpan(
                                    text: '未填写',
                                    style: StylesUtils.customTextStyle(
                                        color: ColorsUtils.colorCccc, fontSize: 15.sp),
                                    children: [
                                      WidgetSpan(
                                          child: Icon(
                                            Icons.arrow_forward_ios,
                                            color: ColorsUtils.parseColorFromHexString(
                                                ColorsUtils.color9999),
                                            size: 15.sp,
                                          ),
                                          alignment: PlaceholderAlignment.middle)
                                    ])))
                          ],
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ));
  }

  void _showActionSheet(BuildContext context) {
    showCupertinoModalPopup<void>(
      context: context,
        builder: (_) => CupertinoActionSheet(
        title: Center(
          child: Text('每月可修改5次，剩余4/5次',
              style: StylesUtils.customTextStyle(fontSize: 13.sp, color: ColorsUtils.color9999)),
        ),
        actions: <Widget>[
          Container(
            color: Colors.white,
            child: CupertinoActionSheetAction(
              isDefaultAction: true,
              isDestructiveAction: true,
              onPressed: () {
                ToolsUtils.cameraImage().then((XFile? file) {
                  if (file != null) {
                    controller.file.value = file;
                  }
                }).catchError((err) {
                  Get.printError(info: 'err = ${err.toString()}');
                }).whenComplete(() {
                  Navigator.pop(context);
                });
              },
              child: Text(
                '拍照',
                style: StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 18.sp),
              ),
            ),
          ),
          Container(
            color: Colors.white,
            child: CupertinoActionSheetAction(
              isDefaultAction: true,
              isDestructiveAction: true,
              onPressed: () {
                ToolsUtils.galleryImage().then((XFile? file) {
                  if (file != null) {
                    controller.file.value = file;
                  }
                  Navigator.pop(context);
                }, onError: (err) {
                  Navigator.pop(context);
                });
              },
              child: Text(
                '从相册选取',
                style: StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 18.sp),
              ),
            ),
          )
        ],
        cancelButton: CupertinoActionSheetAction(
            onPressed: () {
              Navigator.of(context).pop();
            },
            isDestructiveAction: true,
            child: Text(
              '取消',
              style: StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 18.sp),
            )),
      ),
    );
  }
}
